import datetime
import time
import random
import pymysql
import uuid
from faker import Faker

fake = Faker()

# 调用env.py文件中的变量连接数据库
from env import MYSQL_HOST
from env import MYSQL_DATABASE
from env import MYSQL_PASSWORD
from env import MYSQL_USERNAME

MYSQL_HOST = MYSQL_HOST
MYSQL_DATABASE = MYSQL_DATABASE
MYSQL_USERNAME = MYSQL_USERNAME
MYSQL_PASSWORD = MYSQL_PASSWORD

connection = pymysql.connect(host=MYSQL_HOST,
                             database=MYSQL_DATABASE,
                             user=MYSQL_USERNAME,
                             password=MYSQL_PASSWORD)

cursor = connection.cursor()

end_time = datetime.datetime.now()
start_time = datetime.datetime.now() + datetime.timedelta(days=-3650)  # 当前时间减去10年

a1 = tuple(start_time.timetuple()[0:9])  # 设置开始日期时间元组（2020-04-11 16:30:21）
a2 = tuple(end_time.timetuple()[0:9])

start = time.mktime(a1)
end = time.mktime(a2)

user_list = []
sql = "insert into user " \
      "(user_id,user_name,user_email,user_password,create_time) " \
      "values (%s,%s,%s,%s,%s)"

for i in range(777890):
    user_id = uuid.uuid4()
    user_name = str(fake.name())
    user_email = user_name.replace(' ', '').lower() + str(i) + '@gmail.com'
    user_password = fake.password(length=random.randint(10, 16), special_chars=False, digits=True,
                                  upper_case=True, lower_case=True)
    t = random.randint(start, end)
    date_touple = time.localtime(t)  # 将时间戳生成时间元组
    create_date = time.strftime("%Y-%m-%d %H:%M:%S", date_touple)
    args = (user_id, user_name, user_email, user_password, create_date)
    user_list.append(args)
    if i % 10000 == 0:
        cursor.executemany(sql, user_list)
        connection.commit()
        print('Commit:', len(user_list))
        user_list.clear()

cursor.executemany(sql, user_list)
print('Commit:', len(user_list))
connection.commit()
cursor.close()
connection.close()
